#include "qmyFinNal.h"
#include "qizleznifakturivnes.h"
#include "qizleznifakturikorekcija.h"
#include "qpltmvnes.h"
#include "qpltmkorekcija.h"


QMyFinNal::QMyFinNal(QWidget *parent)
    : QMyBaseWidget(parent)
    ,FinNalVnes(0)
    ,FinNalKorekcija(0)
    ,FinNalLista(0)
	,m_row(0)
{
    ui.setupUi(this);
    pressF4();
}

QMyFinNal::~QMyFinNal()
{

}
void QMyFinNal::pressF2()
{
    closeAllForm();
    FinNalVnes = showMyWidget<QFinNalVnes>(FinNalVnes); 
    FinNalVnes->setCategoryWidget(this);
    connect(FinNalVnes,SIGNAL(closeW()),this,SLOT(closeW()));
	connect(FinNalVnes, SIGNAL(scallKontoLista(QWidget*, QString)),this,SLOT(callKontoLista(QWidget*, QString)));
    connect(FinNalVnes,SIGNAL(scallKomintenti(QWidget*, QString)),this,SLOT(callKomintenti(QWidget*, QString)));
	connect(FinNalVnes,SIGNAL(scallIzlezniFakturi(QWidget*, QString)),this,SLOT(callIzlezniFakturi(QWidget*, QString)));
	connect(FinNalVnes,SIGNAL(scallPriemnici(QWidget*, QString)),this,SLOT(callPriemnici(QWidget*, QString)));
	connect(FinNalVnes,SIGNAL(scallVlezniFakturi(QWidget*, QString)),this,SLOT(callVlezniFakturi(QWidget*, QString)));
	connect(FinNalVnes,SIGNAL(scallIspratnici(QWidget*, QString)),this,SLOT(callIspratnici(QWidget*, QString)));
	connect(FinNalVnes,SIGNAL(scallNalogProizvodstvo(QWidget*, QString)),this,SLOT(callNalogProizvodstvo(QWidget*, QString)));
	connect(FinNalVnes, SIGNAL(scallKalkulacija(QWidget*, QString)),this,SLOT(callKalkulacija(QWidget*, QString)));

	emit disablePrint(true);
}
void QMyFinNal::pressF3()
{
    if(!FinNalLista)
    {
        return;
    }
    int id = FinNalLista->getSelectedId();
    closeAllForm();
    FinNalKorekcija = showMyWidget<QFinNalKorekcija>(FinNalKorekcija); 
    FinNalKorekcija->setCategoryWidget(this);
    connect(FinNalKorekcija, SIGNAL(closeW()),this,SLOT(closeW()));
    connect(FinNalKorekcija, SIGNAL(scloseW1()),this,SLOT(closeW1()));
    connect(FinNalKorekcija, SIGNAL(scallKomintenti(QWidget*, QString)),this,SLOT(callKomintenti(QWidget*, QString)));
    connect(FinNalKorekcija, SIGNAL(scallKontoLista(QWidget*, QString)),this,SLOT(callKontoLista(QWidget*, QString)));
	connect(FinNalKorekcija, SIGNAL(scallIzlezniFakturi(QWidget*, QString)),this,SLOT(callIzlezniFakturi(QWidget*, QString)));
	connect(FinNalKorekcija, SIGNAL(scallPriemnici(QWidget*, QString)),this,SLOT(callPriemnici(QWidget*, QString)));
	connect(FinNalKorekcija, SIGNAL(scallVlezniFakturi(QWidget*, QString)),this,SLOT(callVlezniFakturi(QWidget*, QString)));
	connect(FinNalKorekcija, SIGNAL(scallIspratnici(QWidget*, QString)),this,SLOT(callIspratnici(QWidget*, QString)));
	connect(FinNalKorekcija, SIGNAL(scallNalogProizvodstvo(QWidget*, QString)),this,SLOT(callNalogProizvodstvo(QWidget*, QString)));
	connect(FinNalKorekcija, SIGNAL(scallKalkulacija(QWidget*, QString)),this,SLOT(callKalkulacija(QWidget*, QString)));

	FinNalKorekcija->init(id);
	emit disablePrint(true);
}
void QMyFinNal::pressF4()
{
    closeAllForm();
    FinNalLista = showMyWidget<QFinNalLista>(FinNalLista); 
    FinNalLista->setCategoryWidget(this);
	FinNalLista->setRow(m_row);
    connect(FinNalLista,SIGNAL(closeW()),this,SLOT(closeWLista()));
    connect(FinNalLista,SIGNAL(procF2()),this,SLOT(procF2()));
    connect(FinNalLista,SIGNAL(procF3()),this,SLOT(procF3()));
    connect(FinNalLista,SIGNAL(procDel()),this,SLOT(procDel()));
	connect(FinNalLista, SIGNAL(sIspratnicaToIzlFaktura(QString&, QString&)), this, SLOT(retPriemnicaToKalkulacija(QString&,QString&)));

	emit disablePrint(false);
}
void QMyFinNal::procF2()
{
    pressF2();
}
void QMyFinNal::procF3()
{
    pressF3();
}

void QMyFinNal::refresh()
{
}

void QMyFinNal::procDel()
{

}
void QMyFinNal::pressEscape()
{
    this->raise();
    this->setFocus();
	emit disablePrint(true);
    emit signCloseMyWidget();
}
void QMyFinNal::closeW()
{
    pressF4();
}

void QMyFinNal::closeW1()
{
    pressF4();
}

void QMyFinNal::closeWLista()
{
    pressEscape();
}

void QMyFinNal::closeAllForm()
{
    FinNalKorekcija = deleteMyWidget<QFinNalKorekcija>(FinNalKorekcija);
    FinNalVnes = deleteMyWidget<QFinNalVnes>(FinNalVnes);
	if (FinNalLista)
	{
		m_row = FinNalLista->getRow();
	}
    FinNalLista = deleteMyWidget<QFinNalLista>(FinNalLista);
}
void QMyFinNal::callKomintenti(QWidget* myobj, QString text)
{
    emit signKomintentiFromIspratnica(myobj, text);
}


void QMyFinNal::callKontoLista(QWidget* myobj, QString text)
{
	 emit signKontoListaFromNalog(myobj, text);
}
void QMyFinNal::callArtikli(QWidget* myobj, QString text)
{
    emit signArtikliFromIspratnica(myobj, text);
}
///////////////////////////////////////////////////////////////////
void QMyFinNal::callIzlezniFakturi(QWidget* myobj, QString text)
{
	emit signIzlezniFakturi(myobj, text);
}
void QMyFinNal::callPriemnici(QWidget* myobj, QString text)
{
	emit signPriemnici(myobj, text);
}
void QMyFinNal::callVlezniFakturi(QWidget* myobj, QString text)
{
	emit signVlezniFakturi(myobj, text);
}
void QMyFinNal::callIspratnici(QWidget* myobj, QString text)
{
	emit signIspratnici(myobj, text);
}
void QMyFinNal::callNalogProizvodstvo(QWidget* myobj, QString text)
{
	emit signNalogProizvodstvo(myobj, text);
}
void QMyFinNal::callKalkulacija(QWidget* myobj, QString text)
{
	emit signKalkulacija(myobj, text);
}


void QMyFinNal::sendPrintMessage()
{
    emit signPrint();
}


QStandardItemModel* QMyFinNal::getModel()
{
    QStandardItemModel * modelExt;
    if (FinNalLista)
    {
        modelExt = FinNalLista->model;
    } 
    else
    {
        modelExt = NULL;
    }
    return modelExt;
}

QStandardItemModel* QMyFinNal::getModel2()
{
    QStandardItemModel * modelExt;
    if (FinNalLista)
    {
        modelExt = FinNalLista->model2;
    } 
    else
    {
        modelExt = NULL;
    }
    return modelExt;
}

QStringList QMyFinNal::getIspratnicaInfo()
{
	QStringList modelExt;
	modelExt.clear();

	if (FinNalLista)
	{
		modelExt = FinNalLista->geInfo();
	} 
	else
	{
		modelExt;
	}
	return modelExt;
}

bool QMyFinNal::printTovarenList()
{
    bool stat = false;
    if (FinNalLista)
    {
        stat = FinNalLista->isCheckedTovarenList();
    } 
    else
    {
        stat = false;
    }
    return stat;
}

bool QMyFinNal::printMalaIspratnica()
{
	bool stat = false;
	if (FinNalLista)
	{
		stat = FinNalLista->isCheckedMalaIspratnica();
	} 
	else
	{
		stat = false;
	}
	return stat;
}

void QMyFinNal::retPriemnicaToKalkulacija(QString& text, QString& textName)
{
	QWidget *sour = getSourceWidget();

	if (qobject_cast<QIzlezniFakturiVnes*>(sour))
	{
		QIzlezniFakturiVnes *dest = qobject_cast<QIzlezniFakturiVnes*>(sour);
		dest->setIspratnicaID(text);
		emit eupdateNanigator(dest->getCategoryWidget(), this);
		dest->setFocusLine2();
	}
	if (qobject_cast<QIzlezniFakturiKorekcija*>(sour))
	{
		QIzlezniFakturiKorekcija *dest = qobject_cast<QIzlezniFakturiKorekcija*>(sour);
		dest->setIspratnicaID(text);
		emit eupdateNanigator(dest->getCategoryWidget(), this);
		dest->setFocusLine2();
	}

	if (qobject_cast<QPLTMVnes*>(sour))
	{
		QPLTMVnes *dest = qobject_cast<QPLTMVnes*>(sour);
		dest->setIspratnicaID(text);
		emit eupdateNanigator(dest->getCategoryWidget(), this);
		dest->setFocusLine2();
	}
	if (qobject_cast<QPLTMKorekcija*>(sour))
	{
		QPLTMKorekcija *dest = qobject_cast<QPLTMKorekcija*>(sour);
		dest->setIspratnicaID(text);
		emit eupdateNanigator(dest->getCategoryWidget(), this);
		dest->setFocusLine2();
	}

}
